<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layuiAdmin 角色管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
        <button class="layui-btn layui-btn-danger" data-type="batchdel">批量删除</button>
      </div>

      <table id="role_table" lay-filter="role_table"></table>
      <script type="text/html" id="table-useradmin-admin">
        <a class="layui-btn layui-btn-xs" lay-event="permission"><i
                class="layui-icon layui-icon-edit"></i>授权</a>
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table;

        table.render({
            elem: "#role_table",
            url: "/role/getAllRoles",
            cols: [
                [
                    {
                        type: "checkbox",
                        fixed: "left"
                    }, {
                    field: "id",
                    width: 80,
                    title: "ID",
                    sort: true
                }, {
                    field: "name",
                    title: "角色名"
                }, {field: "desc",
                    title: "描述"
                }, {
                    title: "操作",
                    width: 220,
                    align: "center",
                    fixed: "right",
                    toolbar: "#table-useradmin-admin"
                }]
            ],
            text: "对不起，加载出现异常！"
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('role_table')
                    , checkData = checkStatus.data,
                    formData = []; //得到选中的数据
                if (checkData.length == 0){
                    layer.msg("不能为空");
                } else {
                    layer.confirm('是否删除此记录?', {icon: 3, title:'提示'}, function(index){
                        for (var i = 0;i<checkData.length;i++){
                            formData.push(checkData[i].id);
                        }
                        $.ajax({
                            type:"delete",
                            contentType : 'application/json',
                            url:"/delRole/"+formData,
                            data:{},
                            dataType : 'json',
                            success:function(data) {
                                if (data.code == 200){
                                    layer.msg('删除成功',
                                        {
                                            icon:1,
                                            time: 1000
                                        },function(){
                                            table.reload('role_table', {
                                                url : '/role/getAllRoles',
                                                page : {
                                                    curr : 1
                                                }
                                            });
                                            layer.close(index); //关闭弹层
                                        });
                                }else {
                                    layer.msg("删除失败，请检查是否绑定用户");
                                }
                            }
                        });
                    });
                }
            },
            add: function () {
                layer.open({
                    type: 2
                    , title: '添加新角色'
                    , content: '/addRoleUI'
                    , area: ['500px', '450px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find("#addRole");

                        //监听提交
                        iframeWindow.layui.form.on('submit(addRole)', function (data) {
                            var field = data.field; //获取提交的字段
                            $.ajax({
                                type:"put",
                                contentType : 'application/json',
                                url:"/addRole",
                                data:JSON.stringify(field),
                                dataType : 'json',
                                success:function(data) {
                                    if (data.code == 200){
                                        layer.msg('添加成功',
                                            {
                                                icon:1,
                                                time: 1000
                                            },function(){
                                                table.reload('role_table', {
                                                    url : '/role/getAllRoles',
                                                    page : {
                                                        curr : 1
                                                    }
                                                });
                                                layer.close(index); //关闭弹层
                                            });
                                    }else {
                                        layer.msg("后台出错");
                                    }
                                }
                            });
                        });

                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-role').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        table.on('tool(role_table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'permission':
                    layer.open({
                        type: 2
                        , title:'角色授权'
                        , content: '/getPermission/' + data.id
                        , maxmin: true
                        ,  area: ['700px', '450px']
                    });
                    break;
                case 'edit':
                    layer.open({
                        type: 2
                        , title: '编辑角色'
                        , content: '/editRoleUI/' + data.id
                        , maxmin: true
                        , area: ['500px', '450px']
                        , btn: ['保存', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'addRole'
                                , submit = layero.find('iframe').contents().find('#' + submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (d) {
                                var field = d.field; //获取提交的字段
                                $.ajax({
                                    type: "put",
                                    contentType: 'application/json',
                                    url: "/role/updateRole",
                                    data: JSON.stringify(field),
                                    dataType: 'json',
                                    success: function (da) {
                                        if (da.code == 200) {
                                            layer.msg('更新成功',
                                                {
                                                    icon: 1,
                                                    time: 1000
                                                }, function () {
                                                    table.reload('role_table', {
                                                        url: '/role/getAllRoles',
                                                        page: {
                                                            curr: 1
                                                        }
                                                    });
                                                    layer.close(index); //关闭弹层
                                                });
                                        } else {
                                            layer.msg("后台出错");
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                    break;
                case 'del':
                    layer.confirm('是否删除此记录?', {icon: 3, title: '提示'}, function (index) {
                        $.ajax({
                            type: "delete",
                            contentType: 'application/json',
                            url: "/delRole/" + data.id,
                            data: {},
                            dataType: 'json',
                            success: function (data) {
                                if (data.code == 200) {
                                    layer.msg('删除成功',
                                        {
                                            icon: 1,
                                            time: 1000
                                        }, function () {
                                            table.reload('role_table', {
                                                url: '/role/getAllRoles',
                                                page: {
                                                    curr: 1
                                                }
                                            });
                                            layer.close(index); //关闭弹层
                                        });
                                } else {
                                    layer.msg("后台出错");
                                }
                            }
                        });
                    });
                    break;
            }
            ;
        });
    });
</script>
</body>
</html>

